package com.factory.ssm.handler;


import java.io.File;
import java.io.IOException;
import java.util.Date;

import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.factory.ssm.beans.Factory;
import com.factory.ssm.service.FactoryService;
import com.factory.ssm.service.PharmaceuticalService;

import sun.java2d.d3d.D3DScreenUpdateManager;


/**
 * <p>
 *  閸撳秶顏幒褍鍩楅崳锟�
 * </p>
 *
 * @author testjava
 * @since 2020-08-03
 */
@Controller
public class FactoryController {
	
	@Autowired
	FactoryService factoryService;
	
	@Autowired
	PharmaceuticalService pharmaceuticalService;
	
	@RequestMapping(value = "/getFactory",method = RequestMethod.GET)
	public String getFactory(Model model,HttpServletRequest req) {
		System.out.println("jinrule");
		List<Factory> listFactory = factoryService.getFactory();
		System.out.println(listFactory);
		ServletContext applicationContext = req.getSession().getServletContext();
		String imgSavePathe = applicationContext.getRealPath("img");
		String imgSavePath = imgSavePathe+"\\";
		model.addAttribute("imgSavePath", imgSavePath);
		model.addAttribute("listFactory", listFactory);
		System.out.println("aa");
		return "factoryIndex";
	}
	
	@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
	public String deleteFactory(@PathVariable String id) {
		pharmaceuticalService.deleteByFactoryId(id);
		factoryService.deleteFactoryById(id);
		return "redirect:/getFactory";
	}
	
	@RequestMapping(value = "/showAdd",method = RequestMethod.GET)
	public String showAdd() {
		
		return "addFactory";
	}
	
	@RequestMapping(value = "/insert")
	public String insertFactory(Factory factory,@RequestParam(value = "file",required=false) MultipartFile file,HttpServletRequest req) {
		
	
		String fileName = file.getOriginalFilename();
		ServletContext applicationContext = req.getSession().getServletContext();
		String imgSavePath = applicationContext.getRealPath("img");
		String uuid = UUID.randomUUID().toString();
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		String targetFileName = uuid +suffix;
 		File targetFile = new File(imgSavePath+"/"+targetFileName);
 		
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
		factory.setCover(targetFileName);
		factory.setGmtCreate(new Date().toString());
		factory.setGmtModified(new Date().toString());
		factory.setIsDeleted("0");
		factoryService.insert(factory);
		return "redirect:/getFactory";
	}
//	
	@RequestMapping(value = "/showEdit/{id}")
	public String showEditFactory(@PathVariable Integer id,Model model) {
		Factory factory = factoryService.getFactoryById(id);
		model.addAttribute("id", id);
		
		model.addAttribute("factory", factory);
		return "addFactory";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public String editFactory(Factory factory,@PathVariable Integer id,Model model) {
		factory.setId(id);
		pharmaceuticalService.updatePharmaceuticalByFactoryId(factory);
		factoryService.updateFactoryById(factory);
		
		return "redirect:/getFactory";
	}
//	
//	@RequestMapping(value = "/get/pharmaceutical",method = RequestMethod.PUT)
//	public Result updateFactory() {
//		
//		return Result.ok();
//	}

}

